<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Droit extends CI_Controller {

    public function index() {
        $this->layout
                ->title('Accueil')
                //->js('login')
                //->css('test')
                ->view('droit/admin');
    }

    public function admin() {
        $rights = $this->getRights();

        $data['rights'] = $rights;

        $this->layout
                ->title('Administration :: Droits')
                ->js('chosen.jquery')
                ->css('chosen')
                ->user_can('editRights')
                ->view('droit/admin', $data);
    }

    private function getRights($group_id = null) {
        $rights = $this->db
                ->select('*')
                ->from('droit')
                ->get()
                ->result()
        ;

        $arrayRights = array();

        foreach ($rights as $right) {
            $tmp['right_id'] = $right->droit_id;
            $tmp['right_text'] = $right->droit_texte;

            $groupRight = $this->db
                    ->select('*')
                    ->from('droit_groupe')
                    ->where('droit_id', $right->droit_id)
                    ->get()
                    ->result()
            ;

            $groups = $this->db
                    ->select('*')
                    ->from('groupe')
                    ->get()
                    ->result()
            ;
            $tmp['groups'] = array();
            $tmp2 = array();
            foreach ($groups as $group) {
                $tmp2['group_id'] = $group->GROUPE_ID;
                $tmp2['group_libelle'] = $group->GROUPE_LIBELLE;
                $tmp2['group_allowed'] = false;
                foreach ($groupRight as $gr) {
                    if ($gr->group_id == $group->GROUPE_ID && $gr->allowed)
                        $tmp2['group_allowed'] = true;
                }
                array_push($tmp['groups'], $tmp2);
            }
            array_push($arrayRights, $tmp);
        }
        return $arrayRights;
    }

    public function addRight() {
        $name = $this->input->post('name');
        $value = $this->input->post('value');

        $data = array(
            'droit_libelle' => $value,
            'droit_texte' => $name
        );

        $this->db->insert('droit', $data);
    }

    public function saveChanges() {
        $datas = $this->input->post('data');

        $this->db->empty_table('droit_groupe');

        foreach ($datas as $key => $data) {
            $idRight = $key;
            $rights = $data;

            if ($idRight != 0 && is_array($rights)) {
                foreach ($rights as $right) {
                    $data = array(
                        'droit_id' => $idRight,
                        'group_id' => $right,
                        'allowed' => 1
                    );
                    $this->db->insert('droit_groupe', $data);
                }
            }
        }
    }

}